﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System.Net.Http;
using System.Text;

namespace Signit.Sdk.Http
{
    public class JsonContent : StringContent
    {
        public JsonContent(object model) : base(ToJson(model), Encoding.UTF8, "application/json") { }

        private static readonly JsonSerializerSettings defaultJsonSettings = CreateDefaultJsonSettings();

        private static JsonSerializerSettings CreateDefaultJsonSettings()
        {
            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
            };

            settings.Converters.Add(new StringEnumConverter());
            return settings;
        }

        private static string ToJson(object model, JsonSerializerSettings settings = null)
        {
            return JsonConvert.SerializeObject(model, settings ?? defaultJsonSettings);
        }
    }
}
